home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Interactive Reference Guide / C-C++ Interactive Reference Guide.iso / c_ref / csource4 / 203_01 / yam1.c < prev    next >
Text File  |  1979-12-31  |  16KB  |  824 lines

  1. /*
  2. $title ('yam1.c: Main program,command sequencer')
  3. $date (17 nov 85)
  4. */
  5.  
  6. #include "yam.h"
  7. #include <ctype.h>
  8. #define MAXARGS 20
  9.  
  10. /****************************************************************************
  11. FUNCTION:
  12.     main program and command line parser.
  13.  
  14. CALLING PARAMETERS:
  15.     argc:
  16.         count of command line arguments.
  17.     argv:
  18.         pointer to string of command line arguments.
  19. ===========================================================================*/
  20. main(argc, argv)
  21. char *argv[];
  22. int argc;
  23. {
  24.     int c;
  25.     char *args[MAXARGS],
  26.          **argp,
  27.          *nextcmd;
  28.  
  29.         /* initialization */
  30.     nextcmd=NULL;
  31.     onceonly();
  32.  
  33.         /* init parmams and signon */
  34.     init();
  35.  
  36.         /* read command line arguments */
  37.     if(argc>MAXARGS)
  38.         goto usage;
  39.  
  40.         /* copy given arguments to our own array */
  41.     for (c = 0; c < argc; c++)
  42.         args[c] = argv[c];
  43.  
  44.     for(;;)
  45.     {
  46.         if(argc < 2)
  47.         {
  48.             if(nextcmd)
  49.                 cp=nextcmd;
  50.             else
  51.             {
  52. #ifdef XMODEM
  53.                 printf("\nxYAM(%c)", defdisk+'A');
  54. #else
  55.                 printf("\n>>>%c:", defdisk+'a');
  56. #endif
  57.                 gets(cmdbuf);
  58.                 cp=cmdbuf;
  59. #ifdef SYSFUNC
  60.                 if(*cp=='!')
  61.                 {
  62.                     system(++cp);
  63.                     continue;
  64.                 }
  65. #endif
  66.             }
  67.             if(nextcmd=index(';', cp))
  68.                 *nextcmd++ =0;    /*  ; separates commands */
  69.  
  70.                 /* init arg pointer, arg count */
  71.             argp = &args[1];
  72.             argc = 1;
  73.             while(1)
  74.             {
  75.                     /* printable char means vaild argument */
  76.                 if(isgraph(*cp))
  77.                 {
  78.                     *argp++ = cp;
  79.                     argc++;
  80.  
  81.                         /* skip to next arg */
  82.                     while(isgraph(*cp))
  83.                         cp++;
  84.                 }
  85.                     /* skip white space and change to string terminator */
  86.                 while(*cp==' ' || *cp=='\t')
  87.                     *cp++ =0;
  88.  
  89.                     /* found another printable, get next arg */
  90.                 if(isgraph(*cp))
  91.                     continue;
  92.  
  93.                     /* end of argument list */
  94.                 *cp=0;
  95.                 break;
  96.             }
  97.         }
  98.  
  99.             /* loop thru all parmaters */
  100.         for(argp = &args[1]; --argc>0; )
  101.         {
  102.             uncaps(*argp);
  103.             cp = *argp++;
  104.  
  105.                 /* reset mode flags to default. They may have been set
  106.                    by parms in s or r command */
  107.             Gototerm=Batch=Crcflg=Creamfile=Echo=
  108.                 Ctlview=Chat=View=Quiet=FALSE;
  109.  
  110.             blklen=SECSIZ;
  111. #ifdef DEFBYTL
  112.             Bytesleft=DEFBYTL;
  113. #endif
  114.  
  115.                 /* change disk drive */
  116.             if(index(':', cp))
  117.             {
  118.                 chdsk(cp);
  119.                 continue;
  120.             }
  121.  
  122.                 /* evaluate arguments */
  123.             switch(*cp++)
  124.             {
  125.                 /* b [baudrate] */
  126.             case 'b':
  127.  
  128.                     /* bye = exit */
  129.                 if (cmdeq(cp, "ye"))
  130.                     doexit();
  131.  
  132.                     /* break=send break to modem */
  133.                 if(*cp == 'r')
  134.                 {
  135.                     sendbrk();
  136.                     continue;
  137.                 }
  138.  
  139.                     /* set baud rate */
  140.                 else
  141.                 {
  142.                     setbaud(atoi(*argp++));
  143.                     --argc;
  144.                 }
  145.                 continue;
  146.  
  147.             case 'c':
  148. #ifndef RESTRICTED
  149.                 if(cmdeq(cp, "all") && --argc >=1)
  150.                 {
  151.                     uncaps(*argp);    /* make name lcase */
  152.                     if(getphone(*argp,Phone)!=ERROR)
  153.                         dial(Phone);
  154.                     else if (isdigit(**argp))
  155.                         dial_number(*argp);
  156.                     else
  157.                     {
  158.                         printf("Can't find data for %s\n", *argp++);
  159.                         goto usage;
  160.                     }
  161.                     argp++;
  162.                     term();
  163.                     continue;
  164.                 }
  165. #endif
  166.                     /* ch=change config(parity,etc) */
  167.                 else if(cmdeq(cp, "h"))
  168.                 {
  169.                     changecnfg();
  170.                     continue;
  171.                 }
  172.                     /* chat mode */
  173.                 else if(cmdeq(cp, "hat"))
  174.                 {
  175.                     dochat();
  176.                     continue;
  177.                 }
  178.                     /* cd=change directory */
  179.                 else if(cmdeq(cp, "d"))
  180.                 {
  181.                     chgdir(argp);
  182.                     argc=0;
  183.                     continue;
  184.                 }
  185. #ifdef BIGYAM
  186.                     /* crck */
  187.                 else if(cmdeq(cp, "rck"))
  188.                 {
  189.                     docrck( --argc, argp);
  190.                     argc=0;
  191.                     continue;
  192.                 }
  193.  
  194.                     /* crc */
  195.                 else if(cmdeq(cp, "rc"))
  196.                 {
  197.                     docrc( --argc, argp);
  198.                     argc=0;
  199.                     continue;
  200.                 }
  201. #endif
  202.                 else if(cmdeq(cp, "lose"))
  203.                 {
  204.                     dumprxbuff();
  205.                     closerx(TRUE);
  206.                     closetx(TRUE);
  207.                     continue;
  208.                 }
  209.                 goto usage;
  210.  
  211.             case 'd':
  212.                     /* delete file */
  213.                 if(cmdeq(cp, "el"))
  214.                 {
  215. #ifdef RESTRICTED
  216.                     goto nono;
  217. #else
  218.                     doerase( --argc, argp);
  219.                     argc=0;
  220.                     continue;
  221. #endif
  222.                 }
  223.  
  224.                     /* dir */
  225.                 if(cmdeq(cp, "ir"))
  226.                 {
  227.                     dodir(--argc, argp);
  228.                     argc=0;
  229.                     continue;
  230.                 }
  231. #ifdef IBMPC
  232.                     /* debug=cause int 3 */
  233.                 else if(cmdeq(cp, "ebug"))
  234.                 {
  235.                     dbgint();
  236.                     continue;
  237.                 }
  238. #endif
  239.                     /* dump */
  240.                 if(cmdeq(cp, "ump"))
  241.                 {
  242.                     dodump(--argc, argp);
  243.                     argc=0;
  244.                     continue;
  245.                 }
  246.                     /* d.. = set paramter requested false */
  247.                 else if(setval(0)==ERROR)
  248.                     goto usage;
  249.                 continue;
  250.  
  251.             case '-':
  252.             case 'e':
  253.  
  254.                     /* e.. = set paramter requested true */
  255.                 if(setval(~0)==ERROR)
  256.                     goto usage;
  257.                 continue;
  258.  
  259.             case 'f':
  260.                     /* close any already open files */
  261.                 closetx(TRUE);
  262.                     /* set parmaters if any */
  263.                 if(setval(~0)==ERROR)
  264.                     goto usage;
  265.                     /* open file.  open will compute file size */
  266.                 if(--argc<=0 || opentx(1,0,*argp++)==ERROR)
  267.                     goto usage;
  268.                 /* frame file with ^R ^T if Squelch */
  269.                 if(Squelch)
  270.                     sendbyte(022);
  271.                 term();
  272.                 if(Squelch)
  273.                     sendbyte(024);
  274.                 continue;
  275.  
  276.             case 'i':
  277.                 if(cmdeq(cp, "nit"))
  278.                 {
  279.                     init();
  280.                     argc=0;
  281.                     nextcmd=0;    
  282.                     continue;
  283.                 }
  284.                 else
  285.                     goto usage;
  286.  
  287.             case '?':
  288.             case 'h':
  289.                 searchpath(HELPFILE,Utility.ubuf);
  290.                 listfile(NULL,Utility.ubuf);
  291.                 continue;
  292.  
  293.             case 'k':
  294.                 if(cmdeq(cp, "ill"))
  295.                 {
  296.                     clearbuff();
  297.                     continue;
  298.                 }
  299.                 else
  300.                     goto usage;
  301.  
  302. #ifndef RESTRICTED
  303.                     /* change modem port */
  304.             case 'm':
  305.                 closetx(TRUE);
  306.                 if (chngport(atoi(cp))==ERROR)
  307.                     goto usage;
  308.                 continue;
  309. #endif
  310.  
  311.             case 'o':
  312.                     /* turn on DTR to modem */
  313.                 if(cmdeq(cp, "n"))
  314.                 {
  315.                     offhook();
  316.                     continue;
  317.                 }
  318.                     /* hang up phone */
  319.                 onhook();
  320.                 continue;
  321.  
  322.             case 'p':
  323.                     /* pwd=print working directory */
  324.                 if(cmdeq(cp, "wd"))
  325.                     dopwd();
  326.  
  327.                     /* set parmamters */
  328.                 else if(setparm(cp)==ERROR)
  329.                     goto usage;
  330.                 continue;
  331.  
  332.             case 'q':
  333.                 /* quit.  go to DOS, don't hangup */
  334.                 if(cmdeq(cp, "u"))
  335.                 {
  336.                     dumprxbuff();
  337.                     closerx(TRUE);
  338.                     closetx(TRUE);
  339.                     userexit();
  340.                     exit(0);
  341.                 }
  342.                 goto usage;
  343.  
  344.             case 'r':
  345.                 if(cmdeq(cp, "eset"))
  346.                 {
  347.                     terminit();
  348.                         /* close and flush capture buffer */
  349.                     closetx(TRUE);
  350.                     dumprxbuff();
  351.                     closerx(TRUE);
  352.                         /* reset modem port */
  353.                     userinit(TRUE);
  354.  
  355.                     /* change buffer size for debugging */
  356.                     if(--argc>0)
  357.                     {
  358.                         bufend= bufst + atoi(*argp++);
  359.                         clearbuff();
  360.                         dostat();
  361.                     }
  362.                     continue;
  363.                 }
  364.                     /* replot */
  365.                 if(cmdeq(cp, "ep"))
  366.                 {
  367.                     replot(argc>1 ? (--argc,atoi(*argp++)) : 0);
  368.                     continue;
  369.                 }
  370.                     /* rewind */
  371.                 if(cmdeq(cp, "ew"))
  372.                 {
  373.                     rewindcb();
  374.                     continue;
  375.                 }
  376.                     /* set flags for receive */
  377.                 if(chkbvq()==ERROR)
  378.                     goto usage;
  379.                 if(argc<2)
  380.                     Batch=TRUE;
  381.                 psxfer(wcreceive(--argc, argp));
  382.                 if(Gototerm)
  383.                     term();
  384.                 argc=0;
  385.                 continue;
  386.  
  387.             case 's':
  388. #ifdef BIGYAM
  389.                     /* sum */
  390.                 if (cmdeq(cp, "um"))
  391.                 {
  392.                     dosum( --argc, argp);
  393.                     argc=0;
  394.                     continue;
  395.                 }
  396. #endif
  397.  
  398.                     /* s=status */
  399.                 if(*cp==0 && argc==1)
  400.                 {
  401.                     dostat();
  402.                     continue;
  403.                 }
  404.                     /* illegal */
  405.                 if(argc<2 || chkbvq()==ERROR)
  406.                     goto usage;
  407.  
  408.                     /* s *.?= wild card send */
  409.                 if( argc > 2
  410.                   || index('?',*argp) || index('*',*argp))
  411.                     Batch=TRUE;
  412. #ifdef XMODEM
  413.                 /* tell 'em how long it will take! */
  414.                 if(Batch)
  415.                     docomp(argc-1, argp);
  416. #endif
  417.                 /* s filename */
  418.                 psxfer(wcsend(--argc, argp));
  419.                 if(Gototerm)
  420.                     term();
  421.                 argc=0;
  422.                 continue;
  423.  
  424.             case 't':
  425.                     /* type */
  426.                 if(cmdeq(cp, "ype"))
  427.                 {
  428.                     dolist( --argc, argp);
  429.                     argc=0;
  430.                     continue;
  431.                 }
  432.  
  433.                     /* tab = set tab expansion */
  434.                 if(cmdeq(cp, "ab"))
  435.                 {
  436.                     tabstop = (char)(atoi(*argp++));
  437.                     argc=0;
  438.                         /* tab of 0 is illegal.  Also resets
  439.                            tab back to default if tab only entered */
  440.                     if (!tabstop)
  441.                         tabstop=TABS;
  442.                     continue;
  443.                 }
  444.  
  445.                     /* t [filename] */
  446.                 if(--argc > 0)
  447.                 {
  448.                     if(opencapt(*argp++)==ERROR)
  449.                         goto usage;
  450.                 }
  451.                     /* t.. */
  452.                 switch(setval(~0))
  453.                 {
  454.                     case ERROR:
  455.                         goto usage;
  456.                     case OK:
  457.                         term();
  458.                     case TRUE:
  459.                         break;
  460.                         /* remain in command mode if t..c  */
  461.                 }
  462.                 continue;
  463.  
  464.             case 'w':
  465. #ifdef BIGYAM
  466.                     /* word count */
  467.                 if (cmdeq(cp, "c"))
  468.                 {
  469.                     dowc( --argc, argp);
  470.                     argc=0;
  471.                     continue;
  472.                 }
  473. #endif
  474.                 dumprxbuff();
  475.                 continue;
  476.  
  477.             case 'x':
  478.                     /* exit only if x is only command on line */
  479.                 if( *cp==0 && argc==1 )
  480.                     doexit();
  481.  
  482.             case 0:
  483.             default:
  484.                 goto usage;
  485.             }
  486.         }
  487.         continue;
  488. #ifdef RESTRICTED
  489. nono:
  490.         printf("Command not allowed\n");
  491. #endif
  492. usage:
  493.         printf("Type HELP");
  494.         nextcmd=0;
  495.         argc=0;
  496.         continue;
  497.     }
  498. } /* main */
  499.  
  500.  
  501. /****************************************************************************
  502. FUNCTION:
  503.     set flags used by R and S commands.
  504.  
  505. CALLING PARAMETERS:
  506.     none.
  507. ===========================================================================*/
  508. chkbvq()
  509. {
  510.     while(*cp)
  511.         switch(*cp++)
  512.         {
  513.             case 'b':
  514.                 Batch=TRUE;
  515.                 break;
  516.             case 'c':
  517.                 Crcflg=TRUE;
  518.                 break;
  519.             case 'k':
  520.                 blklen=KSIZE;
  521.                 break;
  522.             case 'q':
  523.                 Quiet=TRUE;
  524.                 break;
  525. #ifndef RESTRICTED
  526.             case 't':
  527.                 Gototerm=TRUE;
  528.                 break;
  529.             case 'v':
  530.                 View=TRUE;
  531.                 break;
  532.             case 'y':
  533.                 Creamfile=TRUE;
  534.                 break;
  535. #endif
  536.             default:
  537.                 return ERROR;
  538.         }
  539.     return FALSE;
  540. } /* chkbvq */
  541.  
  542.  
  543. /****************************************************************************
  544. FUNCTION:
  545.     set paramter from an e,d,f or t command.
  546.  
  547. CALLING PARAMETERS:
  548.     value:
  549.         true or false value to set flag to
  550. ===========================================================================*/
  551. setval(value)
  552. unsigned value;
  553. {
  554.     while(*cp)
  555.         switch(*cp++)
  556.         {
  557.             case '!':
  558.                 value = 0;
  559.                 break;
  560. #ifdef Cis02
  561.             case '2':
  562.                 Cis02 = value;
  563.                 break;
  564. #endif
  565.             case 'a':
  566.                 Chat = value;
  567.                 break;
  568.             case 'b':
  569.                 Txeoln = value&TX_BINARY;
  570.                 Txmoname = "BINARY";
  571.                 break;
  572.             case 'd':
  573.                 Dumping = !Squelch || value;
  574.                 break;
  575.             case 'e':
  576.                 Echo = value;
  577.                 break;
  578.             case 'f':
  579.                 Hdx = !value;
  580.                 break;
  581.             case 'g':
  582.                 Txgo = value;
  583.                 break;
  584.             case 'h':
  585.                 Hdx = value;
  586.                 break;
  587.             case 'i':
  588.                 Ignrx = value;
  589.                 break;
  590.             case 'k':
  591.                 blklen = KSIZE;
  592.                 break;
  593.             case 'l':
  594.                 Pflag = value;
  595.                 break;
  596.             case 'n':
  597.                 Txeoln = value&EOL_NL;
  598.                 Txmoname = "NL ONLY";
  599.                 break;
  600.             case 'p':
  601.                 Txeoln = value&EOL_CRPROMPT;
  602.                 Txmoname = "WAIT FOR PROMPT";
  603.                 break;
  604.             case 'r':
  605.                 Txeoln  = value&EOL_CR;
  606.                 Txmoname = "CR ONLY";
  607.                 break;
  608.             case 's':
  609.                 Dumping = !(Squelch=value);
  610.                 break;
  611.             case 't':
  612.                 Waitbunch = value;
  613.                 break;
  614.             case 'v':
  615.                 Ctlview = value;
  616.                 break;
  617.             case 'w':
  618.                 Txeoln = value&EOL_CRWAIT;
  619.                 Txmoname = "CR WAIT FOR NL";
  620.                 break;
  621.             case 'x':
  622.                 Exoneof = value;
  623.                 break;
  624.             case 'z':
  625.                 Zeof = value;
  626.                 break;
  627.             default:
  628.                 return ERROR;
  629.         }
  630.     return OK;
  631. } /* setval */
  632.  
  633.  
  634. /****************************************************************************
  635. FUNCTION:
  636.     Clear the circular buffer without saving it to disk 
  637.  
  638. CALLING PARAMETERS:
  639.     none.
  640. ===========================================================================*/
  641. clearbuff()
  642. {
  643.     Xoffflg=Wrapped=FALSE;
  644.     buffcdq=bufcq=bufcdq=bufpcdq=bufmark=bufst;
  645.     Bufsize=Nfree=bufend-bufst-1;
  646. } /* clearbuff */
  647.  
  648.  
  649. /****************************************************************************
  650. FUNCTION:
  651.     Display status of flags and parameters
  652.  
  653. CALLING PARAMETERS:
  654.     none.
  655. ===========================================================================*/
  656. dostat()
  657. {
  658.         /* name of file being received */
  659.     printf("Receiving %s ",Rfile?Rname:"<nil>");
  660.  
  661.         /* mode of file receive */
  662.     if(Squelch)
  663.         printf("^R ^T Squelch ");
  664.     if(Zeof)
  665.         printf("EOF on ^Z");
  666.     printf("\n");
  667.  
  668.         /* set default mode name */
  669.     if(Txeoln==EOL_NOTHING)
  670.         Txmoname="normal";
  671.  
  672.         /* transmit mode */
  673.     printf("%sSending in %s mode\n", Txgo? "" : "Pausing in ", Txmoname);
  674.     printf("%sWaiting %d loops every %u chars  Pause=%u",
  675.             Waitbunch?"":"NOT ",Throttle, Waitnum, Tpause);
  676.  
  677.     printf(" GOchar=");
  678.     if (GOchar < ' ')
  679.         printf("'^%c'\n",GOchar|0x40);
  680.     else
  681.         printf("'%c'\n");
  682.  
  683.         /* printer mode and port mode */
  684.     printf("Printer:%s Duplex:%s Tabstop:%d\n",
  685.              Pflag?"ON":"OFF", Hdx?"HALF":"FULL",tabstop);
  686.     printf("baud:%u data port:%d\n", Baudrate, commport);
  687.  
  688.         /* buffer stats */
  689.     printf("%u of %u chars used %u free%s\n",
  690.       Bufsize-Nfree, Bufsize+1, Nfree+1, Wrapped?" POINTERS WRAPPED":"");
  691.  
  692.         /* buffer stats for debug */
  693. /*    printf("bufst=%x bufcq=%x bufcdq=%x buffcdq=%x bufpcdq=%x bufend=%x\n",
  694.      bufst, bufcq, bufcdq, buffcdq, bufpcdq, bufend);
  695. */
  696.         /* total errors in tx, rx */
  697.     if (toterrs)
  698.     {
  699.         printf("%u Total errors detected\n", toterrs);
  700.         toterrs = 0;
  701.     }
  702.         /* print space left on disk */
  703.     printdfr();
  704. } /* dostat */
  705.  
  706.  
  707. /****************************************************************************
  708. FUNCTION:
  709.     index returns a pointer to the first occurrence of c in s,
  710.     NULL otherwise.
  711.  
  712. CALLING PARAMETERS:
  713.     c:
  714.         character to look for
  715.     *s:
  716.         pointer to string to search.
  717. ===========================================================================*/
  718. char *index(c, s)
  719. char c,*s;
  720. {
  721.     for(; *s; s++)
  722.         if(c== *s)
  723.             return s;
  724.     return NULL;
  725. }
  726.  
  727.  
  728. /****************************************************************************
  729. FUNCTION:
  730.     display status of transfer
  731.  
  732. CALLING PARAMETERS:
  733.     status:
  734. ===========================================================================*/
  735. psxfer(status)
  736. int status;
  737. {
  738. #ifdef XREM
  739.     printf("File transfer(s) ");
  740. #else
  741.     printf("\007File transfer(s) ");
  742. #endif
  743.     if (status==ERROR)
  744.         printf("ABORTED BY ERROR\n\007");
  745.     else
  746.         printf("successful\n");
  747.  
  748.         /* print space left on disk */
  749.     printdfr();
  750. } /* psxfer */
  751.  
  752.  
  753. /****************************************************************************
  754. FUNCTION:
  755.     set paramter from a 'p' command.
  756.  
  757. CALLING PARAMETERS:
  758.     *p:
  759.         pointer to string of parmaters to set
  760. ===========================================================================*/
  761. setparm(p)
  762. char *p;
  763. {
  764.     if ( !isdigit(p[1]))
  765.         return ERROR;
  766.     firstch = atoi(p + 1);        
  767.     switch(*p)
  768.     {
  769.     case 'g':
  770.         GOchar = firstch;
  771.         break;
  772.     case 'p':
  773.         Tpause = firstch;
  774.         break;
  775.     case 't':
  776.         Throttle = firstch;
  777.         break;
  778.     case 'w':
  779.         Waitnum = firstch;
  780.         break;
  781.     default:
  782.         return ERROR;
  783.     }
  784.     return 0;
  785. } /* setparm */
  786.  
  787.  
  788. /****************************************************************************
  789. FUNCTION:
  790.     make string s lower case
  791.  
  792. CALLING PARAMETERS:
  793.     *s:
  794.         pointer to string to convert to upper case.
  795. ===========================================================================*/
  796. uncaps(s)
  797. char *s;
  798. {
  799.     for( ; *s; ++s)
  800.         *s = tolower(*s);
  801. }
  802.  
  803.  
  804. /****************************************************************************
  805. FUNCTION:
  806.     perform exit cleanup to return to dos
  807.  
  808. CALLING PARAMETERS:
  809.     none
  810. ===========================================================================*/
  811. doexit()
  812. {
  813.         /* hang up phone */
  814.     onhook();
  815.         /* dump capture buffer */
  816.     dumprxbuff();
  817.         /* close xmit and rx files */
  818.     closerx(TRUE);
  819.     closetx(TRUE);
  820.     purgeline();
  821.     userexit();
  822.     exit(0);
  823. }
  824.